package com.scribbles.article.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.scribbles.article.controller.ArticleController;
import com.scribbles.article.dto.ArticleHeaderDTO;
import com.scribbles.common.dto.ArticleDTO;
import com.scribbles.common.entity.Article;
import com.scribbles.common.entity.ArticleHeader;

@Component
public class EntityConversionUtil {
	private Logger logger = Logger.getLogger(ArticleController.class);

	public ArticleDTO convertArticleEntityToArticleDTO(Article article) {
		ArticleDTO articleDTO = new ArticleDTO();
		articleDTO.setId(article.getId());
		articleDTO.setName(article.getName());
		articleDTO.setDescription(article.getDescription());
		articleDTO.setArticleContent(article.getArticleContent());
		return articleDTO;
	}
	
	public ArticleHeaderDTO convertArticleHeaderEntityToArticleHeaderDTO(ArticleHeader articleHeader) {
		ArticleHeaderDTO articleHeaderDTO = new ArticleHeaderDTO();
		articleHeaderDTO.setId(articleHeader.getId());
		articleHeaderDTO.setName(articleHeader.getName());
		articleHeaderDTO.setStatus(articleHeader.getStatus());
		articleHeaderDTO.setCreatedBy(articleHeader.getCreatedBy().getFirstName() + " " + articleHeader.getCreatedBy().getLastName());
		articleHeaderDTO.setCreatedDate(articleHeader.getCreatedDate());
		return articleHeaderDTO;
	}

	public List<ArticleDTO> convertArticleEntityListToArticleDTOList(
			List<Article> articlesList) {
		List<ArticleDTO> articlesDTOList = new ArrayList<ArticleDTO>();
		
		for (Article article : articlesList) {
			ArticleDTO articleDTO = convertArticleEntityToArticleDTO(article);
			articlesDTOList.add(articleDTO);			
		}
		return articlesDTOList;
	}
}
